<template>
  <!-- 全局引入TinyMCE -->

  <el-dialog
    v-model="editDialogVisible"
    title="修改经销商信息"
    width="40%"
    center
    @close="closeDialog(false)"
  >
    <el-form
      ref="validateForm"
      :model="customerForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="customerForm.name"></el-input>
      </el-form-item>
      <el-form-item label="手机/电话" prop="phone">
        <el-input v-model="customerForm.phone"></el-input>
      </el-form-item>

      <el-form-item label="客户类型" prop="fromType">
        <el-select
          v-model="customerForm.fromType"
          placeholder="请选择"
          @change="fromTypeChange"
        >
          <el-option
            v-for="item in fromTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="经销商" prop="productId" v-if="dealerFlag === true">
        <el-select
          v-model="customerForm.ownDealerId"
          filterable
          placeholder="选择经销商"
        >
          <el-option
            v-for="item in dealerList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系状态" prop="connetState">
        <el-select v-model="customerForm.connetState" placeholder="请选择">
          <el-option
            v-for="item in connetStates"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属区域" prop="ownArea">
        <el-cascader
          v-model="customerArea"
          placeholder="查询或选择区域"
          :options="areas"
          filterable
          :props="areasProps"
        />
      </el-form-item>

      <el-form-item label="详细地址" prop="address">
        <el-input v-model="customerForm.address"></el-input>
      </el-form-item>

      <el-form-item label="身份证" prop="idcard">
        <el-input v-model="customerForm.idcard"></el-input>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="customerForm.remark"></el-input
      ></el-form-item>
      <el-form-item>
        <el-button @click="validateLogin" type="primary">修改</el-button>
        <el-button
          @click="
            () => {
              closeDialog(false);
            }
          "
          >取消</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { reactive, ref, toRefs, onMounted } from "vue";
import { ElNotification } from "element-plus";
import { getRequest, putRequest } from "../../utils/api";
import region from "../../utils/region";

function useLoadFormData(state) {
  getRequest("/sell/customer/one", { id: state.customerId }).then((res) => {
    if (res && res.code == 200) {
      state.customerForm = res.data;
      if (res.data.ownArea) {
        state.customerArea = res.data.ownArea.split(",");
      }else{
        state.customerArea = [];
      }
    } else {
      ElNotification({
        type: "error",
        title: "失败",
        message: "查询失败",
        duration: 2000,
      });
    }

    // state.imageUrl = res.data.coverImg;
    //  editor.value.setHTML(res.data.customerDetail);
  });
}

export default {
  components: {},
  props: {
    editDialogVisible: Boolean,
    customerId: String,
  },
  setup(props, { emit }) {
    const validateForm = ref();
    const rules = {
      name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
      phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
    };
    const state = reactive({
      editDialogVisible: props.editDialogVisible,
      dealerFlag: false,
      customerArea: ["河北省", "邯郸市", "成安县"],
      dealerList: [],
      customerId: props.customerId,
      connetStates: [
        {
          value: 0,
          label: "待沟通",
        },
        {
          value: 1,
          label: "沟通中",
        },
        {
          value: 2,
          label: "已下单",
        },
        {
          value: 3,
          label: "战败",
        },
      ],
      fromTypes: [
        {
          value: 0,
          label: "直销",
        },
        {
          value: 1,
          label: "经销商销售",
        },
      ],
      customerForm: {
        name: "",
        phone: "",
        username: "",
        address: "",
        remark: "",
        connetState: 1,
        fromType: 0,
        ownArea: "",
        ownDealerId: "",
        idcard: "",
        id: props.customerId,
        isFlag:false 
      },
      areasProps: { children: "children", label: "name", value: "name" },
      areas: region,
    });

    const closeDialog = (visible, data) => {
      emit("onEditCloseDialog", visible, data);
    };
    const validateLogin = () => {
      validateForm.value.validate((valid) => {
        if (state.customerArea) {
          state.customerForm.ownArea = state.customerArea.toString();
        }
        putRequest("/sell/customer/depository/", state.customerForm).then((res) => {
          if (res && res.code == 200) {
            validateForm.value.resetFields();
            emit("onEditCloseDialog", false, res.data);
          } else {
            ElNotification({
              type: "error",
              title: "失败",
              message: res.message,
              duration: 2000,
            });
          }
        });
      });
    };
    const fromTypeChange = (val) => {
      if (val == 1) {
        state.dealerFlag = true;
      } else {
        state.dealerFlag = false;
        state.customerForm.ownDealerId = "";
      }
    };

    onMounted(() => {});
    onMounted(() => {
      useLoadFormData(state);
      //加载供应商信息
      getRequest("/sell/dealer/listAll").then((response) => {
        if (response) {
          state.dealerList = response.data;
        }
      });
    });
    return {
      ...toRefs(state),
      fromTypeChange,
      rules,
      // handleAvatarSuccess,
      //解构customerAdd返回的结果  返回submitAdd对象函数 需要把富文本编辑器也传过去 这里省略了
      validateForm,
      closeDialog,
      validateLogin,
    };
  },
};
</script>

<style>
.el-form-item__error {
  padding-top: 0px;
}
.el-input__inner {
  height: 32px;
  line-height: 32px;
}
.el-form-item {
  margin-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 18px;
  color: #8c939d;
  width: 88px;
  height: 88px;
  line-height: 88px;
  text-align: center;
}
.avatar {
  width: 88px;
  height: 88px;
  display: block;
}
</style>